package com.cap.dreamcircle.View;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.DreamLabelBean;
import com.cap.dreamcircle.Model.Bean.DreamLabelEntity;
import com.cap.dreamcircle.Model.IDreamModel;
import com.cap.dreamcircle.Model.Impl.DreamModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.Utils.TransformUtils;
import com.cap.dreamcircle.Widget.FlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action0;

/**
 * Created by liucxu on 2017/8/22.
 */

public class DreamLabelActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.layout_back)
    LinearLayout layout_back;
    @BindView(R.id.tv_change_page)
    TextView tv_change_page;
    @BindView(R.id.flow_layout)
    FlowLayout flow_layout;
    @BindView(R.id.layout_query)
    RelativeLayout layout_query;

    private int page_index = 1;
    private IDreamModel model;

    @Override
    public int getLayoutId() {
        return R.layout.activity_dream_label;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        tv_title.setText("解梦");
    }

    @Override
    public void setupData() {
        model = new DreamModelImpl();
        requestLabels();
    }

    private void requestLabels() {
        model.RequestDreamLabel(page_index + "").compose(TransformUtils.<DreamLabelBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new CommonSubscriber<DreamLabelBean>() {
            @Override
            public void onSuccess(DreamLabelBean response) {
                hideProgress();
                if ((response.getTagArray() == null) || (response.getTagArray().size() == 0)) {
                    App.GetInstance().finishActivity(DreamLabelActivity.this);
                } else {
                    refreshLabels(response.getTagArray());
                }
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                ToastUtils.showShortToast(DreamLabelActivity.this, response.getDesc());
            }
        });
    }

    private void refreshLabels(List<DreamLabelEntity> labels) {
        flow_layout.clearSubViews();
        for (int index = 0; index < labels.size(); index++) {
            View label_view = View.inflate(DreamLabelActivity.this, R.layout.dream_label_view, null);
            TextView tv_label = (TextView) label_view.findViewById(R.id.tv_label);
            final String tagId = labels.get(index).getId() + "";
            final String tagName = labels.get(index).getTagName();
            tv_label.setText(labels.get(index).getTagName());
            label_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyCounterUtils.SendKeyCounter("257dcab2edf1415587412768e9d7460b");
                    Intent intent = new Intent(DreamLabelActivity.this, DreamLabelStreamActivity.class);
                    intent.putExtra("tagId", tagId);
                    intent.putExtra("tagName", tagName);
                    startActivity(intent);
                }
            });
            flow_layout.addView(label_view);
        }
    }

    @OnClick({R.id.layout_back, R.id.tv_change_page, R.id.layout_query})
    void doClicks(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.layout_back:
                App.GetInstance().finishActivity(DreamLabelActivity.this);
                break;
            case R.id.tv_change_page:
                KeyCounterUtils.SendKeyCounter("596fd97466d346018ab68fd71cb9fced");
                page_index = page_index + 1;
                requestLabels();
                break;
            case R.id.layout_query:
                Intent intent = new Intent(this, SearchDreamLabelActivity.class);
                startActivity(intent);
                break;
        }
    }
}
